package com.jinbooks.web.marketing.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jinbooks.entity.marketing.MarketingCampaign;
import com.jinbooks.persistence.service.MarketingCampaignService;
import com.jinbooks.entity.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 营销活动Controller控制器
 */
@RestController
@RequestMapping("/marketing/campaign")
public class MarketingCampaignController {
    
    @Autowired
    private MarketingCampaignService marketingCampaignService;
    
    /**
     * 保存营销活动（草稿）
     *
     * @param marketingCampaign 营销活动实体
     * @return Message 统一返回结果
     */
    @PostMapping("/save")
    public Message<Boolean> saveCampaign(@RequestBody MarketingCampaign marketingCampaign) {
        boolean result = marketingCampaignService.saveCampaign(marketingCampaign);
        if (result) {
            return new Message<>(Message.SUCCESS, "保存成功");
        } else {
            return new Message<>(Message.FAIL, "保存失败");
        }
    }
    
    /**
     * 保存并执行营销活动
     *
     * @param marketingCampaign 营销活动实体
     * @return Message 统一返回结果
     */
    @PostMapping("/saveAndExecute")
    public Message<Boolean> saveAndExecuteCampaign(@RequestBody MarketingCampaign marketingCampaign) {
        boolean result = marketingCampaignService.saveAndExecuteCampaign(marketingCampaign);
        if (result) {
            return new Message<>(Message.SUCCESS, "保存并执行成功");
        } else {
            return new Message<>(Message.FAIL, "保存并执行失败");
        }
    }
    
    /**
     * 分页查询营销活动
     *
     * @param page 分页对象
     * @param marketingCampaign 查询条件
     * @return Message 统一返回结果
     */
    @GetMapping("/fetch")
    public Message<IPage<MarketingCampaign>> pageList(Page<MarketingCampaign> page, MarketingCampaign marketingCampaign) {
        return new Message<>(Message.SUCCESS, marketingCampaignService.pageList(page, marketingCampaign));
    }
    
    /**
     * 根据ID获取营销活动详情
     *
     * @param id 营销活动ID
     * @return Message 统一返回结果
     */
    @GetMapping("/{id}")
    public Message<MarketingCampaign> getCampaignById(@PathVariable Long id) {
        return new Message<>(Message.SUCCESS, marketingCampaignService.getCampaignById(id));
    }
    
    /**
     * 更新营销活动
     *
     * @param marketingCampaign 营销活动实体
     * @return Message 统一返回结果
     */
    @PutMapping("/update")
    public Message<Boolean> updateCampaign(@RequestBody MarketingCampaign marketingCampaign) {
        boolean result = marketingCampaignService.updateCampaign(marketingCampaign);
        if (result) {
            return new Message<>(Message.SUCCESS, "更新成功");
        } else {
            return new Message<>(Message.FAIL, "更新失败");
        }
    }
    
    /**
     * 更新并执行营销活动
     *
     * @param marketingCampaign 营销活动实体
     * @return Message 统一返回结果
     */
    @PutMapping("/saveAndExecute")
    public Message updateAndExecuteCampaign(@RequestBody MarketingCampaign marketingCampaign) {
        // 先更新营销活动
        boolean updateResult = marketingCampaignService.updateCampaign(marketingCampaign);
        if (!updateResult) {
            return Message.failed("更新失败");
        }
        
        // 更新成功后再执行营销活动
        try {
            marketingCampaignService.scheduleCampaignExecution(marketingCampaign);
            return Message.ok("更新并执行成功");
        } catch (Exception e) {
            return Message.failed("执行失败: " + e.getMessage());
        }
    }
    
    /**
     * 根据ID删除营销活动
     *
     * @param id 营销活动ID
     * @return Message 统一返回结果
     */
    @DeleteMapping("/{id}")
    public Message<Boolean> deleteCampaignById(@PathVariable Long id) {
        boolean result = marketingCampaignService.deleteCampaignById(id);
        if (result) {
            return new Message<>(Message.SUCCESS, "删除成功");
        } else {
            return new Message<>(Message.FAIL, "删除失败");
        }
    }
    
    /**
     * 批量删除营销活动
     *
     * @param ids 营销活动ID列表
     * @return Message 统一返回结果
     */
    @DeleteMapping("/batch")
    public Message<Boolean> deleteCampaignByIds(@RequestBody List<Long> ids) {
        boolean result = marketingCampaignService.deleteCampaignByIds(ids);
        if (result) {
            return new Message<>(Message.SUCCESS, "批量删除成功");
        } else {
            return new Message<>(Message.FAIL, "批量删除失败");
        }
    }
}